package com.sanhuo.security.service;

import com.sanhuo.lib.ExceptionUtil;
import com.sanhuo.security.UserVo;
import com.sanhuo.security.bo.PasswordUpdateBO;
import com.sanhuo.security.bo.UserUpdateBO;
import com.sanhuo.security.dao.UserMapper;
import com.sanhuo.security.entity.User;
import com.sanhuo.security.uitls.UserHolder;
import com.sanhuo.triild.Basic.BaseMapper;
import com.sanhuo.triild.Basic.BaseService;
import com.sanhuo.triild.annotation.MapperWire;
import com.sanhuo.triild.page.Page;
import com.sanhuo.triild.page.PageSearch;
import com.sanhuo.utils.basic.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserService extends BaseService<User> {
    @MapperWire
    private UserMapper userMapper;

    @Override
    public BaseMapper<User> getBaseMapper() {
        return userMapper;
    }


    public void updateInfo(User user) throws IllegalAccessException {
        User oriUser = userMapper.findById(user.getId());
        BeanUtil.copyBean(user, oriUser);
        userMapper.update(oriUser);
    }

    public void updatePassword(PasswordUpdateBO bo) {
        UserVo oriUser = UserHolder.user();
        ExceptionUtil.throwException(!oriUser.getPassword().equals(bo.getOriPassword()), "旧密码输入错误!");
        User user = userMapper.findById(oriUser.getId());
        user.setPassword(bo.getNewPassword());
        user.updatorInfo(oriUser);
        userMapper.updatePassword(user);
    }

    public Page<User> listAll(PageSearch pageSearch) {
        return userMapper.listAll(pageSearch);
    }

    public void update(UserUpdateBO bo) {
        userMapper.update(bo);
    }



}
